function install_py_libs() {
    # 安装构建依赖
    # requirements_txt：流水线自定义参数
    txt_file=requirements.txt
    echo -e $requirements_txt > $txt_file
    cat $txt_file
    pip install -r $txt_file
    sleep 1
    # python是模板容器自定义安装的，根据创建流水线选择的模板确定
    ls -alh /opt/tools/python/3.8.18/Python-3.8.18/bin | grep sphinx
    # 安装sphinx无法执行build命令，需要进行设置PATH变量
    export PATH=/opt/tools/python/3.8.18/Python-3.8.18/bin:$PATH
}


function check_success() {
    if [ $? -eq 0 ]; then
        echo "$1 命令执行成功！"
    else
        echo "$1 命令执行失败！"
        exit 1
    fi
}

# 克隆代码
function main() {
    # WEBHOOK_PAYLOAD：流水线系统参数，gitee的webhook触发回调传参
    gitee_action=$(echo $WEBHOOK_PAYLOAD | jq '.action')
    if [ -z "${gitee_action}" ]; then
        echo "WEBHOOK_PAYLOAD data not found!"
        exit 1
    else
        # 触发的分支/tag名称，TARGET_BRANCH 流水线系统变量
        export gitee_branch=$TARGET_BRANCH

        # jq解析webhook的json数据获取值
        is_created=$(echo $WEBHOOK_PAYLOAD | jq -r '.created')
        is_deleted=$(echo $WEBHOOK_PAYLOAD | jq -r '.deleted')
        hook_name=$(echo $WEBHOOK_PAYLOAD | jq -r '.hook_name')
        echo "构建webhook类型：${hook_name}, created: ${is_created}, deleted: ${is_deleted}"

        if [ "${hook_name}" == "merge_request_hooks" ]; then
            # PR合入
            # 获取target_branch和state即可
            state=$(echo $WEBHOOK_PAYLOAD | jq -r '.state')
            # target_branch=$(echo $WEBHOOK_PAYLOAD | jq -r '.target_branch')
            if [ "${state}" != "merged" ]; then
                # 可能是pr新建、更新等
                echo "PR未合入，忽略本次构建"
                exit 0
            fi
        elif [ "${hook_name}" == "tag_push_hooks" ]; then
            # tag新建或者删除
            if [ "${is_created}" == "true" ]; then
                echo "tag新增操作"
                # tag新增分支需要覆盖为TAG 流水线系统变量
                export gitee_branch=$TAG
            else
                echo "非tag新增操作，忽略本次构建"
                exit 0
            fi
        elif [ "${hook_name}" == "push_hooks" ]; then
            # 代码合入、分支增删
            if [ "${is_created}" == "false" -a "${is_deleted}" == "true" ]; then
                echo "分支删除操作，忽略本次构建"
                exit 0
            elif [ "${is_created}" == "true" ]; then
                echo "新建分支操作"
            else
                echo "代码合入操作"
            fi
        else
            echo "未适配的webhook数据，忽略本次构建"
            exit 1
        fi

        export gitee_namespace=$(echo $WEBHOOK_PAYLOAD | jq -r '.repository.namespace')

        # gitee路径使用小写
        export gitee_repo_name=$(echo $WEBHOOK_PAYLOAD | jq -r '.repository.path')

        private_repo=$(echo $WEBHOOK_PAYLOAD | jq -r '.repository.private')
        echo "当前构建仓库属性private：${private_repo}"

        echo "组织：${gitee_namespace}，仓名：${gitee_repo_name}，分支/tag ${gitee_branch}"


        # 文档路径，默认都是仓目录下面的docs文件夹，还需要适配是否存在中英文文件夹
        docs_dir="docs"
        echo "文档路径: ${docs_dir}"
        if [ ! -d "${docs_dir}" ]; then
            echo "文档路径不存在，构建失败"
            exit 1
        fi

        install_py_libs
        # 设置环境变量，上传obs使用
        export obs_upload_dir="${gitee_branch}"
        sphinx-build -b html ${docs_dir} ${obs_upload_dir}
        check_success "sphinx构建"

        # 语言版本适配
        # export language='zh_CN'
        # 如hispark/芯片/语言/版本
        # export obs_object_key="${obs_root_key}/${gitee_repo_name}/${language}/${gitee_branch}"

        # obs上传的objectKey
        if [ -z "${obs_root_key}" ]; then
            export obs_object_key="${gitee_repo_name}/${gitee_branch}"
        else
            export obs_object_key="${obs_root_key}/${gitee_repo_name}/${gitee_branch}"
        fi
        echo "OBS配置，待上传文件夹：${obs_upload_dir}, 声明objectKey：${obs_object_key}"
         echo -e $obs_upload_py > obs_upload.py
         python obs_upload.py
         check_success "OBS上传"
    fi
}


main